<template>
  <div class="order-list">
    <order-header title="订单列表">
      <template v-slot:tip>
        <span class="tips">请谨防钓鱼链接或诈骗电话，了解更多></span>
      </template>
    </order-header>
    <loading v-if="loading"></loading>
    <div class="wrapper">
      <div class="container">
        <div class="order-box">
          <div
            class="order"
            v-for="(item, i) in orderList"
            :key="i"
          >
            <div class="order-title">
              <div class="item-info fl">
                {{item.createTime}}
                <span>|</span>
                {{item.receiverName}}
                <span>|</span>
                订单号：{{item.orderNo}}
                <span>|</span>
                在线支付
              </div>
              <div class="item-money fr">
                <span>应付金额：</span>
                <span class="money">{{item.payment}}</span>
                <span>元</span>
              </div>
            </div>
            <div class="order-content clearfix">
              <div class="good-box fl">
                <div class="good-list"
                  v-for="(pro,index) in item.orderItemVoList"
                  :key="index"
                >
                  <div class="good-img">
                    <img v-lazy="pro.productImage" alt="">
                  </div>
                  <div class="good-name">
                    <div class="p-name">{{pro.productName}}</div>
                    <div class="p-money">{{pro.currentUnitPrice}} X {{pro.quantity}}元</div>
                  </div>
                </div>
              </div>
              <div class="good-state fr" v-if="item.tatus === 20">
                <a href="javascript:;">{{item.statusDesc}}</a>
              </div>
              <div class="good-state fr" v-else>
                <a href="javascript:;" @click="goPay(item.orderNo)">{{item.statusDesc}}</a>
              </div>
            </div>
          </div>
          <el-pagination
            layout="prev, pager, next"
            :pageNum="pageNum"
            :pageSize="pageSize"
            :total="total"
            @current-change="handleChange"
            background
          >
          </el-pagination>
          <div class="btn-loadmore" v-if="false">
            <el-button type="primary" :loading="loading" @click="loadMore">加载更多</el-button>
          </div>
          <div
            class="scoll-loadmore"
            v-infinite-scroll="scollLoadMore"
            infinite-scroll-disabled="busy"
            infinite-scroll-distance="410"
            v-if="false"
          >
            <img src="/imgs/loading-svg/loading-spinning-bubbles.svg" v-show="showLoadMore">
          </div>
        </div>
      </div>
      <no-data v-if="!loading && orderList.length == 0"></no-data>
    </div>
  </div>
</template>
<script>
  import { orderList } from './../../util/request.js'
  import OrderHeader from './../../components/OrderHeader'
  import Loading from './../../components/Loading'
  import NoData from './../../components/Nodata'
  import infiniteScroll from 'vue-infinite-scroll' 
  export default{
    name:'order-list',
    directives: {infiniteScroll},
    data() {
      return {
        orderList: [], // 订单列表
        loading: false,
        pageNum: 1,
        pageSize: 5,
        total: 0,
        showLoadMore: true, // 按钮分页器是否显示
        busy: false, // 控制是否滚动加载， false为滚动加载，true为禁止滚动加载
      }
    },
    components:{
      OrderHeader,
      Loading,
      NoData
    },
    methods: {
      getOrderList() {
        this.loading = true
        let params = {
          pageSize: this.pageSize,
            pageNum: this.pageNum
        }
        orderList(params).then(res => {
          this.loading = false
          if (res.hasNextPage === false) {
            this.showLoadMore = false
            return this.$message.warning('订单列表已全部加载')
          }
          this.orderList = res.list || []
          this.total = res.total
        }).catch(() => {
          this.loading = false
        })
      },
      goPay(orderNo) {
        this.$router.push({
          path: '/order/pay',
          query: {
            orderNo: orderNo
          }
        })
      },
      // 分页器加载
      handleChange(pageNum) {
        this.pageNum = pageNum
        this.getOrderList()
      },
      // 更多按钮加载
      loadMore() {
        this.pageNum++
        this.getOrderList()
      },
      // 滚动加载
      scollLoadMore() {
        this.busy = true // 触发方法禁止滚动加载
        setTimeout(() => {
          this.pageNum++
          this.scollGetOrderList()
          // 加载完毕后可以继续滚动加载
          this.busy = false
        }, 500)
      },
      // 滚动加载专用
      scollGetOrderList() {
        let params = {
          pageSize: this.pageSize,
            pageNum: this.pageNum
        }
        orderList(params).then(res => {
          this.orderList = this.orderList.concat(res.list) || []
          this.total = res.total
          if(res.hasNextPage){
            this.busy=false;
          }else{
            this.showLoadMore = false
            this.busy=true;
          }
        })
      }
    },
    mounted() {
      this.getOrderList()
    }
  }
</script>
<style lang="scss">
  @import './../../assets/scss/config.scss';
  @import './../../assets/scss/mixin.scss';
  .order-list{
    .wrapper{
      background-color:$colorJ;
      padding-top:33px;
      padding-bottom:110px;
      .order-box{
        .order{
          @include border();
          background-color:$colorG;
          margin-bottom:31px;
          &:last-child{
            margin-bottom:0;
          }
          .order-title{
            @include height(74px);
            background-color:$colorK;
            padding:0 43px;
            font-size:16px;
            color:$colorC;
            .item-info{
              span{
                margin:0 9px;
              }
            }
            .money{
              font-size:26px;
              color:$colorB;
            }
          }
          .order-content{
            padding:0 43px;
            .good-box{
              width:88%;
              .good-list{
                display: flex;
                align-items: center;
                height:145px;
                .good-img{
                  width:112px;
                  img{
                    width:100%;
                  }
                }
                .good-name{
                  font-size:20px;
                  color:$colorB;
                }
              }
            }
            .good-state, .cancel-pay{
              @include height(145px);
              font-size: 20px;
              color:$colorA;
              a{
                color:$colorA;
              }
            }
          }
        }
        .pagination{
          text-align:right;
        }
        .el-pagination.is-background .el-pager li:not(.disabled).active{
          background-color: #FF6600;
        }
        .btn-loadmore, .scoll-loadmore{
          width: 100%;
          text-align: center;
        }
        .el-button--primary{
          background-color: #FF6600;
          border-color: #FF6600;
        }
        .load-more,.scroll-more{
          text-align:center;
        }
      }
    }
  }
</style>